package member.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import member.service.IMemberService;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import common.action.BaseAction;
import common.pojo.Member;
import common.pojo.MemberInfo;
import common.pojo.MemberOtherInfo;
import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;
import static common.utils.Constants.*;
@Scope("prototype")
@Controller("member")
public class MemberAction extends BaseAction {
	/**
 * 
 */
	private static final long serialVersionUID = 1L;

	/**
	 * 分页查询用户组
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getMemberListByPage() throws Exception {
		String totalTmp = request.getParameter("total");
		String currentTmp = request.getParameter("current");
		int total;
		int current;
		if (totalTmp == null)
			total = memberService.getCount();
		else
			total = Integer.valueOf(request.getParameter("total"));
		if (currentTmp == null)
			current = 1;
		else
			current = Integer.valueOf(request.getParameter("current"));
		Page page = PageUtil.createPage(LIMIT, total, current);
		members = memberService.getMemberListByPage(page.getBeginIndex(), LIMIT, "member.id", "desc");
		pagination = PaginationUtil.getPaginationStyleTwo(page,"member!getMemberListByPage.action");
		return "memberListByPage";
	}

	/**
	 * 新建
	 * 
	 * @return
	 * @throws IOException 
	 */
	public String save() throws IOException {
		memberService.save(member, memberInfo, new MemberOtherInfo());
		return "success";
	}
	
	public void checkUser() throws Exception{	
		String name=request.getParameter("name");
		Member memb = memberService.findByName(name);
		if (memb != null) {
			response.getWriter().write("{\"success\":false}");
		} else {
			response.getWriter().write("{\"success\":true}");
		}
	}
	
	public String findInfoById() {
		int id = Integer.valueOf(request.getParameter("id"));
		member = memberService.findMemberById(id);
		memberInfo = memberService.findInfoById(id);
		memberOtherInfo = memberService.findOtherInfoById(id);
		return "findSuccess";
	}


	public String deleteByIds() {
		String ids = request.getParameter("ids");
		memberService.delete(ids);
		return "success";
	}

	/**
	 * 更新
	 * 
	 * @return
	 */
	public String update() {
		String rePwd = request.getParameter("repassword");
		memberService.update(member, memberInfo,  rePwd);
		return "success";
	}
	
	
	@Resource
	private IMemberService memberService;
	private List<Member> members;
	private String pagination;
	private Member member;
	private MemberInfo memberInfo;
	private MemberOtherInfo memberOtherInfo;
	private String dofunction;
	
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public MemberOtherInfo getMemberOtherInfo() {
		return memberOtherInfo;
	}

	public void setMemberOtherInfo(MemberOtherInfo memberOtherInfo) {
		this.memberOtherInfo = memberOtherInfo;
	}

	public String getDofunction() {
		return dofunction;
	}


	public void setDofunction(String dofunction) {
		this.dofunction = dofunction;
	}


	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}



	public List<Member> getMembers() {
		return members;
	}

	public void setMembers(List<Member> members) {
		this.members = members;
	}

	public MemberInfo getMemberInfo() {
		return memberInfo;
	}

	public void setMemberInfo(MemberInfo memberInfo) {
		this.memberInfo = memberInfo;
	}
}
